<style>
    .layui-form-checked[lay-skin=primary] i {
        background-color: #5FB878;
    }

    .layui-textarea {
        min-height: 80px;
    }

    #items .del {
        float: right;
        color: #00a2d4;
    }
</style>
<div class="fsh-pop">
    <input type="password" style="display: none"/>
    <div id="add_form" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label"><i>*</i>类型</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="0" title="多选" checked="">
                <input type="radio" name="sex" value="1" title="单选">
            </div>
        </div>
        <div id="items">
            <div class="layui-form-item">
                <label class="layui-form-label"><i>*</i>题目</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" name="content" lay-verify="content"></textarea>
                </div>

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <label id="btn-add-item"><i class="iconfont icon-add"></i><a>继续添加选项</a></label>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">附件</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="upload_header">上传</button>
                    <div class="layui-upload-list">
                        <input name="resource_url" type="hidden" id="resUrl">
                    </div>
                </div>
            </div>

        </div>
    </div>

</div>

<script type="text/javascript">
    layui.use(['upload', 'form'], function () {
        var upload = layui.upload;
        var form = layui.form;
        for (var i = 0; i < 4; i++) {
            appendItem();
        }
        //选项
        $("#btn-add-item").click(function () {
            appendItem();
            form.render();
        });
        $("#parentTitle").click(function () {
            showDialog({
                title: '选择父级菜单'
                , template: 'menu'
                , width: 400
                , height: 600
                , yes: function (index, layero) {
                    var res = callbackdata();
                    layer.close(index);
                    $("#parentTitle").val(res.title);
                    $("#parent_id").val(res.key);
                }

            });

        });
        //执行实例
        var uploadInst = upload.render({
            elem: '#upload_header'
            , url: context + '/api/fileupload/add'
            , field: 'attach'
            , done: function (res) {
                //如果上传失败
                if (res.success) {
                    // $('#demo1').attr('src', res.link);
                    $('#resUrl').val(res.link);
                } else {
                    layui.layer.msg('上传失败');
                }
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });

    function appendItem() {
        var items = $("#items");
        var index = items.children().length;
        var item = '            <div class="layui-form-item" id="item'+index+'">\n' +
            '                <label class="layui-form-label"><i>*</i>选项' + (index) + '</label>\n' +
            '                <div class="layui-input-block">\n' +
            '                    <textarea class="layui-textarea" name="content" lay-verify="content" ></textarea>\n' +
            '                    <input type="checkbox" name="like[write]" lay-skin="primary" title="设为正确答案"/>\n' +
            '                    <label class="layui-form-label del" id="del'+index+'"><a>删除</a></label>\n' +
            '                </div>\n' +
            '            </div>';
        items.append(item);
        $("#del"+index).click(function () {
            $("#item"+index).remove();
        });
    }
</script>
